/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.broker.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.ourgrid.common.interfaces.AccountingAggregator;
import org.ourgrid.common.spec.peer.PeerSpec;
import org.ourgrid.reqtrace.Req;

import br.edu.ufcg.lsd.commune.Application;
import br.edu.ufcg.lsd.commune.container.servicemanager.dao.DAO;
import br.edu.ufcg.lsd.commune.identification.ContainerID;

/**
 * Maintains information about the current 
 * workers providers of this Broker.
 * 
 */
public class PeerDAO extends DAO {

	private final Map<ContainerID, PeerEntry> currentPeers;
	
	private final Map<ContainerID, AccountingAggregator> accountingAggregators;
	
	public PeerDAO(Application application) {
		super(application);
		this.currentPeers = new HashMap<ContainerID, PeerEntry>();
		this.accountingAggregators = new HashMap<ContainerID, AccountingAggregator>();
	}

	@Req("REQ309")
	public void setPeers(Collection<PeerSpec> peers) {
		
		currentPeers.clear();
		
		for (PeerSpec peerSpec : peers) {
			currentPeers.put(peerSpec.getServiceID().getContainerID(), 
					new PeerEntry(peerSpec));
		}
	}
	
	public Collection<ContainerID> getPeersIDs() {
		return new ArrayList<ContainerID>(this.currentPeers.keySet());
	}
	
	public Collection<PeerEntry> getPeers() {
		return new ArrayList<PeerEntry>(this.currentPeers.values());
	}

	public boolean containsPeer(ContainerID peerID) {
		return this.currentPeers.containsKey(peerID);
	}

	/**
	 * @param entityID
	 * @return
	 */
	public PeerEntry getPeerEntry(ContainerID peerID) {
		return this.currentPeers.get(peerID);
	}

	public boolean hasPeerUp() {
		for (PeerEntry peerEntry : currentPeers.values()) {
			if (!peerEntry.isDown()) {
				return true;
			}
		}
		
		return false;
	}

	public Collection<PeerEntry> getLoggedPeersEntries() {
		Collection<PeerEntry> loggedPeers = new ArrayList<PeerEntry>();
		
		for (PeerEntry entry : this.currentPeers.values()) {
			if (entry.isLogged()) {
				loggedPeers.add(entry);
			}
		}
		
		return loggedPeers;
	}
	
	public void addAccountingAggregator(ContainerID peerID, AccountingAggregator aggregator) {
		this.accountingAggregators.put(peerID, aggregator);
	}
	
	public AccountingAggregator getAccountingAgreggator(ContainerID peerID) {
		return this.accountingAggregators.get(peerID);
	}

	public void removePeer(ContainerID peerID) {
		this.currentPeers.remove(peerID);
		
	}
	
}
